package com.qzdsoft.erpcloud.controller.sys;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.domain.sys.SysRole;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysMenuButtonService;
import com.qzdsoft.erpcloud.service.sys.SysRoleMenuService;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.sys.SysRoleInfo;
import com.qzdsoft.erpcloud.vo.sys.SysRoleQueryParam;
import com.qzdsoft.erpcloud.vo.sys.ZtreeNode;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysRoleStatus;

/**
 * Created by Admin
 * 系统角色
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
	private static final String BASEPATH = "sys/role";

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuButtonService sysMenuButtonService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    
    @Autowired
    private SysUserService sysUserService;
    
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
    	model.addAttribute("commonStatus2List", SysRoleStatus.ALL);
//        model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());

        return BASEPATH+"/index";
    }

    /**
     *
     * @param request
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<SysRoleInfo> data(HttpServletRequest request, SysRoleQueryParam param) {
//        Integer count = sysRoleService.selectDataCount(param.getName(), param.getStatus());
//        List list = sysRoleService.selectData(param.getName(), param.getStatus(), param.getiDisplayStart(), param.getiDisplayLength());
    	param.setCompanyId(0);
    	if(param.isFirst()) {
    		return LayTableResponse.nullDataPage();
    	}
        return sysRoleService.selectAllData(param);
    }
    
    /**
     * 新增角色
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView addinit() {
    	
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("commonStatus2List", SysRoleStatus.ALL);
    	mv.setViewName(BASEPATH+"/add");
    	return mv;
    }
    /**
     * 执行新增保存
     * @param request
     * @param sysRole
     * @return
     */
    @SysLog(description="系统角色新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Object add(SysRole sysRole) {
        LoginUser user = getUser();
        sysRole.setType(0);
        return sysRoleService.saveOrUpd(sysRole);
    }
    
    /**
     * 编辑数据
     * @param request
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
    	logger.debug("系统角色修改id={}",id);
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("commonStatus2List", SysRoleStatus.ALL);
    	mv.addObject("obj",sysRoleService.selectById(id));
    	mv.setViewName(BASEPATH+"/edit");
        return mv;
    }

    /**
     * 执行编辑
     * @param request
     * @param sysRole
     * @return
     */
    @SysLog(description="系统角色修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Object editExecute(SysRole sysRole) {
        LoginUser user = getUser();
        return sysRoleService.saveOrUpd(sysRole);
    }

    /**
     * 删除
     * @param request
     * @param id
     * @return
     */
    @SysLog(description="系统角色删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return sysRoleService.del(id);
    }
    
    /**
     * 打开ztree页面
     * @param Id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/openTree")
    public String opendTree(Model model,Integer id){
    	model.addAttribute("roleId", id);
    	return BASEPATH+"/treeNode";
    }
    
    /**
     *  菜单树
     *
     * @param request
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/findAllMenu")
    public Response<ZtreeNode> findAllMenu(Integer id) {
    	LoginUser user = getUser();
    	Integer uid = user.getId();
    	return new Response(ResultEnum.SUCCESS,sysMenuButtonService.findByRoleId(id,uid));
    }
    
    /**
     * 菜单权限保存
     */
    @SysLog(description="系统角色权限修改保存")
    @ResponseBody
    @RequestMapping("/roleinit")
    public Response roleinit(Integer roleId,String id) {
    	logger.debug("权限保存:roleId={},id={}",roleId,id);
    	
    	return sysRoleService.roleInt(roleId, id);
    }
}
